package com.czd.support.controller;

import com.czd.base.model.JsonResult;
import com.czd.base.utils.WebUtils;
import com.czd.support.common.Constants;
import com.czd.support.model.DataDictionary;
import com.czd.support.model.Manager;
import com.czd.support.model.Phrasebook;
import com.czd.support.service.DataDictionaryService;
import com.czd.support.service.PhrasebookService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014-11-22 15:26)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/phrasebook")
public class PhrasebookController extends BaseController {
    @Autowired
    private PhrasebookService phrasebookService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    /**
     * 题目 类型字典
     * @return
     */
    @ModelAttribute("phrasebookTypeList")
    protected List<DataDictionary> initQuestionType(){
        return dataDictionaryService.queryListByCode(Constants.PHRASEBOOK_CODE);
    }

    @RequestMapping(value = {"","/","/list"},method = RequestMethod.GET)
    public String list(Model model,Phrasebook phrasebook,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
        phrasebook.setPageNo(pageNo);
        model.addAttribute("paginate",phrasebookService.queryForPaginate(phrasebook));
        return "phrasebook/list";
    }

    /**
     * 新增短语控制器
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "phrasebook/add";
    }

    /**
     * 录入短语 拦截
     * @param phrasebook
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String save(Phrasebook phrasebook,HttpServletRequest request,Model model){
        Manager manager = this.getSesionUser(request);
        phrasebook.setCreator(manager.getUserCode());
        phrasebook.setUpdator(manager.getUserCode());
        if(StringUtils.isBlank(phrasebook.getContent())){
            return  error(model, "操作提示", "短语不许为空");
        }
        if(phrasebookService.checkContent(phrasebook)){
            return error(model,"编辑异常","短语不允许重复");
        }
        if(phrasebookService.insertPhrasebook(phrasebook)){
            return "redirect:/phrasebook/"+phrasebook.getId()+"/details";
        }
        return  error(model, "操作提示", "短语录入失败");
    }

    /**
     * 题目详情页 拦截
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/details",method = RequestMethod.GET)
    public String view(@PathVariable int id,Model model){
        Phrasebook phrasebook = phrasebookService.findById(id);
        if(phrasebook != null){
            model.addAttribute("phrasebook",phrasebook);
            return "phrasebook/details";
        }
        return error(model,"信息不存在","短语不存在");
    }

    /**
     * 题目详情页 拦截
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String edit(@PathVariable int id,Model model){
        Phrasebook phrasebook = phrasebookService.findById(id);
        if(phrasebook != null){
            model.addAttribute("phrasebook",phrasebook);
            return "phrasebook/edit";
        }
        return error(model,"信息不存在","短语不存在");
    }


    /**
     * 题目详情页 拦截
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/edit",method = RequestMethod.POST)
    public String update(@PathVariable int id,Phrasebook phrasebook,HttpServletRequest request,Model model){
        Manager manager = this.getSesionUser(request);
        phrasebook.setUpdator(manager.getUserCode());
        phrasebook.setId(id);
        if(phrasebookService.checkContent(phrasebook)){
            return error(model,"编辑异常","短语不允许重复");
        }
        if(phrasebookService.updatePhrasebook(phrasebook)){
            return "redirect:/phrasebook/"+phrasebook.getId()+"/details";
        }
        return error(model,"信息不存在","短语不存在");
    }

    @RequestMapping(value = "/{id}/checkContent")
    @ResponseBody
    public JsonResult check(@PathVariable int id,HttpServletRequest request){
        String content = WebUtils.findParamStr(request,"content");
        Phrasebook phrasebook = new Phrasebook();
        phrasebook.setId(id);
        phrasebook.setContent(content);
        if(phrasebookService.checkContent(phrasebook)){
            return JsonResult.FAIL;
        }
        return JsonResult.OK;
    }
}
